﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class EditPatientAssignmentForm : Form
    {
        private int patientId;
        private Control control;

        public EditPatientAssignmentForm(int id)
        {
            patientId = id;
            control = Control.getInstance();
            InitializeComponent();
            InitializeContents();
        }

        public void InitializeContents()
        {
            System.Data.Odbc.OdbcDataReader result = control.ExecuteQuery("SELECT imie, nazwisko FROM pacjent WHERE id_pacjent = " + patientId + ";");
            if (!result.Read())
            {
                MessageBox.Show("Wybrany pacjent nie istnieje w bazie danych!", "Pacjent nie istnieje", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.Text = "Zmień termin zapisu: " + result.GetString(0) + " " + result.GetString(1);
            LB_PatientInfo.Text = "Pacjent: " + result.GetString(0) + " " + result.GetString(1);

            CB_ChooseTermin.Items.Clear();
            result = control.ExecuteQuery("SELECT id_lekarza, data_wizyty FROM pacjent_zapis WHERE id_pacjenta = " + patientId + ";");
            if (!result.HasRows)
            {
                TerminInfo t = new TerminInfo();
                t.LekarzId = -1;
                t.info = "-- Pacjent nie ma żadnych umówionych wizyt --";
                CB_ChooseTermin.Items.Add(t);
            }
            else
            {
                TerminInfo t = new TerminInfo();
                t.LekarzId = -1;
                t.info = "-- Wybierz termin, który chcesz zmienić --";
                CB_ChooseTermin.Items.Add(t);

                while (result.Read())
                {
                    t = new TerminInfo();
                    System.Data.Odbc.OdbcDataReader result2 = control.ExecuteQuery("SELECT id, first_name, last_name FROM staff WHERE id = " + result.GetInt32(0) + ";");
                    if (result2.Read())
                    {
                        t.LekarzId = result2.GetInt32(0);
                        t.czasFull = result.GetString(1);
                        System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(" ");
                        String[] czas = regex.Split(result.GetString(1));
                        t.czas = new LekarzTime(czas[1]);
                        t.info = result.GetString(1) + " - Lekarz:" + result2.GetString(1) + " " + result2.GetString(2);
                        CB_ChooseTermin.Items.Add(t);
                    }
                }
            }
        }

        private void OnFormClose(object sender, EventArgs e)
        {
            Control.getInstance().CloseWindow(this);
        }

        private void CB_ChooseTermin_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CB_ChooseTermin.SelectedIndex.Equals(0))
            {
                BT_Change.Visible = false;
                BT_Delete.Visible = false;
            }
            else
            {
                BT_Change.Visible = true;
                BT_Delete.Visible = true;
            }
        }

        private void BT_Change_Click(object sender, EventArgs e)
        {
            if (CB_ChooseTermin.SelectedIndex.Equals(0))
                return;
            
            TerminInfo t = (TerminInfo)CB_ChooseTermin.SelectedItem;
            AssignPatientForm patientForm = new AssignPatientForm(patientId, t);
            Control.getInstance().SwitchWindow(this, patientForm);
        }

        private void BT_Delete_Click(object sender, EventArgs e)
        {
            if (CB_ChooseTermin.SelectedIndex.Equals(0))
                return;

            TerminInfo t = (TerminInfo)CB_ChooseTermin.SelectedItem;

            System.Data.Odbc.OdbcDataReader result = control.ExecuteQuery("SELECT imie, nazwisko FROM pacjent WHERE id_pacjent = " + patientId + ";");
            result.Read();

            DialogResult delete = MessageBox.Show("Czy na pewno chcesz usunąć termin zapisu pacjenta: " +
                                                  result.GetString(0) + " " +
                                                  result.GetString(1) + "\nna termin " +
                                                  t.ToString(),
                                                  "Potwierdz usuniecie",
                                                  MessageBoxButtons.YesNo,
                                                  MessageBoxIcon.Question);

            if (delete.Equals(DialogResult.Yes))
            {
                String connStr = "DELETE FROM pacjent_zapis WHERE id_pacjenta = " + patientId + " AND id_lekarza = " + t.LekarzId +
                                 " AND data_wizyty = '" + t.czasFull + "';";
                control.ExecuteStmt(connStr);
                MessageBox.Show("Pomyślnie usunięto termin wizyty", "Operacja pomyślna", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
    }

    public class TerminInfo
    {
        public int LekarzId;
        public LekarzTime czas;
        public String czasFull;
        public String info;

        public override String ToString()
        {
            return info;
        }
    }
}
